/**
 * 
 */
package com.dhcc.cdbp.query;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 前缀查询，类似于数据库SQL里的like 'java%'，查询以指定前缀字符串打头的索引文档
 * 	注意事项：
 * 		1.使用Query query = new PrefixQuery(new Term(fieldName,queryString))时，queryString不需要加*星号，
 * 但是使用Query query = parser.parse("lucene*")时需要加*星号
 * 		2.queryString根据分析器来规定大小写
 * @author 谢进军
 *
 */
public class PrefixQueryTest {
	private static final String fieldName = "contents";
	
	private static final String values = "Javax";
	
	private static String prefix = "java";  
	
	private Directory directory = null;
	@Before public void setUp() throws Exception {
		directory = new RAMDirectory();
		IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer());
		iwc.setOpenMode(OpenMode.CREATE);
		IndexWriter writer = new IndexWriter(directory, iwc);
		Document document = new Document();
		document.add(new StringField(fieldName, values,Store.YES));
		writer.addDocument(document);
		writer.close();
	}
	@Test public void testTermQuery() throws IOException {
		List<Document> result = new ArrayList<>();
		IndexReader reader = DirectoryReader.open(directory);
		assertEquals(1, reader.maxDoc());
		IndexSearcher searcher = new IndexSearcher(reader);
		Query query = new PrefixQuery( new Term(fieldName,prefix));
		TopDocs docs = searcher.search(query, 10);
		ScoreDoc[] hits = docs.scoreDocs;
		for (ScoreDoc doc : hits) {
			int docId = doc.doc;
			Document document = searcher.doc(docId);
			result.add(document);
		}
		reader.close();
		for( Document document : result){
			System.out.println(document.get(fieldName));
		}
	}
	@Test public void testParseQuery() throws IOException, ParseException {
		List<Document> result = new ArrayList<>();
		IndexReader reader = DirectoryReader.open(directory);
		assertEquals(1, reader.maxDoc());
		IndexSearcher searcher = new IndexSearcher(reader);
		QueryParser parser = new QueryParser(fieldName, new StandardAnalyzer());
		Query query = parser.parse(prefix + "*");
		TopDocs docs = searcher.search(query, 10);
		ScoreDoc[] hits = docs.scoreDocs;
		for (ScoreDoc doc : hits) {
			int docId = doc.doc;
			Document document = searcher.doc(docId);
			result.add(document);
		}
		reader.close();
		for( Document document : result){
			System.out.println(document.get(fieldName));
		}
	}
	@After public void tearDown() throws Exception {
		directory.close();
	}
}
